package cn.iocoder.yudao.module.team.controller.admin.orde;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.team.controller.admin.orde.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.orde.OrdeDO;
import cn.iocoder.yudao.module.team.service.orde.OrdeService;

@Tag(name = "管理后台 - 团队订单")
@RestController
@RequestMapping("/team/orde")
@Validated
public class OrdeController {

    @Resource
    private OrdeService ordeService;

    @PostMapping("/create")
    @Operation(summary = "创建团队订单")
    @PreAuthorize("@ss.hasPermission('team:orde:create')")
    public CommonResult<Integer> createOrde(@Valid @RequestBody OrdeSaveReqVO createReqVO) {
        return success(ordeService.createOrde(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新团队订单")
    @PreAuthorize("@ss.hasPermission('team:orde:update')")
    public CommonResult<Boolean> updateOrde(@Valid @RequestBody OrdeSaveReqVO updateReqVO) {
        ordeService.updateOrde(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除团队订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('team:orde:delete')")
    public CommonResult<Boolean> deleteOrde(@RequestParam("id") Integer id) {
        ordeService.deleteOrde(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除团队订单")
                @PreAuthorize("@ss.hasPermission('team:orde:delete')")
    public CommonResult<Boolean> deleteOrdeList(@RequestParam("ids") List<Integer> ids) {
        ordeService.deleteOrdeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得团队订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('team:orde:query')")
    public CommonResult<OrdeRespVO> getOrde(@RequestParam("id") Integer id) {
        OrdeDO orde = ordeService.getOrde(id);
        return success(BeanUtils.toBean(orde, OrdeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得团队订单分页")
    @PreAuthorize("@ss.hasPermission('team:orde:query')")
    public CommonResult<PageResult<OrdeRespVO>> getOrdePage(@Valid OrdePageReqVO pageReqVO) {
        PageResult<OrdeDO> pageResult = ordeService.getOrdePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrdeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出团队订单 Excel")
    @PreAuthorize("@ss.hasPermission('team:orde:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrdeExcel(@Valid OrdePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrdeDO> list = ordeService.getOrdePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "团队订单.xls", "数据", OrdeRespVO.class,
                        BeanUtils.toBean(list, OrdeRespVO.class));
    }

}